﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;

namespace LuWeiboSDK
{
    public class ParameterHelper
    {
        /// <summary>
        /// 从地址字符串中解析参数
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static List<Parameter> GetParametersFromUrl(string url)
        {
            List<Parameter> parameters = new List<Parameter>();
            string parasStr = url.Split('?')[1];
            string[] paras = parasStr.Split('&');
            foreach (string para in paras)
            {
                string[] keyvalues = para.Split('=');
                string key = keyvalues[0];
                string value = keyvalues[1];
                Parameter parameter = new Parameter(key, value);
                parameters.Add(parameter);
            }
            return parameters;
        }
        /// <summary>
        /// 根据参数生成请求url
        /// </summary>
        /// <param name="urlHeader"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static string BuildParameters(List<Parameter> paras)
        {
            StringBuilder sb = new StringBuilder();
            //paras.Add(new Parameter("source", Constant.Appkey));
            paras.Add(new Parameter("access_token", HttpUtility.UrlEncode(Constant.AccessToken)));
            foreach (Parameter para in paras)
            {
                sb.AppendFormat("&{0}={1}", para.Key, para.Value);
            }
            return sb.ToString().Substring(1);
        }
    }
}
